{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'pear'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表list\n",
    "fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']\n",
    "#计数\n",
    "print(fruits.count('apple'))\n",
    "#索引\n",
    "fruits.index('banana')\n",
    "fruits.index('banana', 4)  # Find next banana starting a position 4\n",
    "#反转\n",
    "fruits.reverse()\n",
    "print(fruits)\n",
    "#追加元素\n",
    "fruits.append('grape')\n",
    "print(fruits)\n",
    "#排序\n",
    "fruits.sort()\n",
    "print(fruits)\n",
    "#弹出元素\n",
    "fruits.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
      "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n",
      "[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]\n",
      "[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]\n"
     ]
    }
   ],
   "source": [
    "#列表推导式\n",
    "#对序列或可迭代对象中的每个元素应用某种操作，用生成的结果创建新的列表；或用满足特定条件的元素创建子序列\n",
    "squares = []\n",
    "for x in range(10):\n",
    "    squares.append(x**2)\n",
    "print(squares)\n",
    "#使用列表推导式：\n",
    "squares = [x**2 for x in range(10)]\n",
    "print(squares)\n",
    "\n",
    "#列表推导式的方括号内包含以下内容：一个表达式，后面为一个 for 子句，然后，是零个或多个 for 或 if 子句。\n",
    "combs=[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]\n",
    "print(combs)\n",
    "#等价：\n",
    "combs = []\n",
    "for x in [1,2,3]:\n",
    "    for y in [3,1,4]:\n",
    "        if x != y:\n",
    "            combs.append((x, y))\n",
    "print(combs)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12345\n",
      "(12345, 54321, 'hello!')\n",
      "((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [8]\u001b[0m, in \u001b[0;36m<cell line: 12>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[38;5;28mprint\u001b[39m(u)\n\u001b[1;32m     11\u001b[0m \u001b[38;5;66;03m# Tuples are immutable:\u001b[39;00m\n\u001b[0;32m---> 12\u001b[0m t[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m88888\u001b[39m\n\u001b[1;32m     15\u001b[0m \u001b[38;5;66;03m# but they can contain mutable objects:\u001b[39;00m\n\u001b[1;32m     16\u001b[0m v \u001b[38;5;241m=\u001b[39m ([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m], [\u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m1\u001b[39m])\n",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "#元组\n",
    "t = 12345, 54321, 'hello!'\n",
    "print(t[0])\n",
    "\n",
    "print(t)\n",
    "\n",
    "# Tuples may be nested:\n",
    "u = t, (1, 2, 3, 4, 5)\n",
    "print(u)\n",
    "\n",
    "# Tuples are immutable:\n",
    "t[0] = 88888\n",
    "\n",
    "\n",
    "# but they can contain mutable objects:\n",
    "v = ([1, 2, 3], [3, 2, 1])\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 集合set是由不重复元素组成的无序容器。基本用法包括成员检测、消除重复元素。集合对象支持合集、交集、差集、对称差分等数学运算\n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)                      # show that duplicates have been removed\n",
    "\n",
    "print('orange' in basket)              # fast membership testing\n",
    "\n",
    "print('crabgrass' in basket)\n",
    "\n",
    "\n",
    "# Demonstrate set operations on unique letters from two words\n",
    "\n",
    "a = set('abracadabra')\n",
    "b = set('alacazam')\n",
    "print(a)                                  # unique letters in a\n",
    "\n",
    "print(a - b)                              # letters in a but not in b\n",
    "\n",
    "print(a | b)                             # letters in a or b or both\n",
    "\n",
    "print(a & b)                             # letters in both a and b\n",
    "\n",
    "print(a ^ b)                           # letters in a or b but not both\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.2 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
